package com.vincent.kdp.adapter;

import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.response.UncheckGoodsListOutput;
import com.vincent.kdp.ui.other.UncheckGoodsListActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class UncheckGoodsListAdapter extends Adapter<ViewHolder> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private UncheckGoodsListActivity context;
//    private List<UncheckGoodsListOutput.DataBean.ListBean> data;


    public UncheckGoodsListAdapter(UncheckGoodsListActivity context, List data) {
        this.context = context;
//        this.data = data;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
//        return data.size() == 0 ? 0 : data.size() + 1;
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.activity_item_uncheck_goods_list, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.d("VIEW_HOLDER",position+"");
//        if(position<data.size()){
//            UncheckGoodsListOutput.DataBean.ListBean c = data.get(position);
//            if (holder instanceof ItemViewHolder) {
//                ItemViewHolder itemViewHolder=(ItemViewHolder)holder;
//
//                itemViewHolder.mBarCode.setText("条码："+c.getBarCode());
//                itemViewHolder.mName.setText("标签："+c.getName());
//
//                if("1".equals(c.getChargeUnit())){
//                    itemViewHolder.mChargeUnit.setText("单/克："+c.getTotalWeight()+"g");
//                }else{
//                    itemViewHolder.mChargeUnit.setText("单/克：¥"+c.getPrice()+"元");
//                }
//
//                if (onItemClickListener != null) {
//                    holder.itemView.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            int position = holder.getLayoutPosition();
//                            onItemClickListener.onItemClick(holder.itemView, position);
//                        }
//                    });
//
//                    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                        @Override
//                        public boolean onLongClick(View v) {
//                            int position = holder.getLayoutPosition();
//                            onItemClickListener.onItemLongClick(holder.itemView, position);
//                            return false;
//                        }
//                    });
//                }
//            }
//        }else{
//            if(holder instanceof FootViewHolder){
//                FootViewHolder footViewHolder=(FootViewHolder)holder;
//                if(!context.hasNext){
//                    footViewHolder.progressBar.setVisibility(View.INVISIBLE);
//                    footViewHolder.textView.setText("没有更多数据");
//                }else{
//                    footViewHolder.progressBar.setVisibility(View.VISIBLE);
//                    footViewHolder.textView.setText("加载中");
//                }
//            }
//        }
    }


    static class ItemViewHolder extends ViewHolder {

        @Bind(R.id.charge_unit)
        TextView mChargeUnit;
        @Bind(R.id.name)
        TextView mName;
        @Bind(R.id.bar_code)
        TextView mBarCode;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class FootViewHolder extends ViewHolder {

        ProgressBar progressBar;
        TextView textView;

        public FootViewHolder(View view) {
            super(view);
            progressBar=(ProgressBar) view.findViewById(R.id.progressBar);
            textView=(TextView) view.findViewById(R.id.footText);
        }
    }
}